﻿using Microsoft.Phone.Controls;
using SmarterCharger.App.ViewModel;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Navigation;

namespace SmarterCharger.App.Views
{
    public partial class StartChargingBatteryPage : PhoneApplicationPage
    {
        private StartChargingBatteryViewModel ViewModel
        {
            get { return DataContext as StartChargingBatteryViewModel; }
            set { DataContext = value; }
        }

        public StartChargingBatteryPage()
        {
            InitializeComponent();

            ViewModel = ContainerInitializer.Get<StartChargingBatteryViewModel>();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            RemoveFirstTimeUserPageFromHistory();
            AnimateScreen();
            ViewModel.LoadData();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            ViewModel.SaveData();
        }

        private void RemoveFirstTimeUserPageFromHistory()
        {
            var isFirstTimeUserPagePrevious = (NavigationService.CanGoBack
                && NavigationService.BackStack.Last().Source.ToString().EndsWith(ViewNames.FirstTimeUserPageName));

            if (isFirstTimeUserPagePrevious)
                NavigationService.RemoveBackEntry();
        }

        private void AnimateScreen()
        {
            var slideLeft = new SlideTransition { Mode = SlideTransitionMode.SlideLeftFadeIn };
            var instructionsTransition = slideLeft.GetTransition(Instructions);
            instructionsTransition.Completed += delegate { instructionsTransition.Stop(); };
            instructionsTransition.Begin();

            var slideRight = new SlideTransition { Mode = SlideTransitionMode.SlideRightFadeIn };
            var smartPlugTransition = slideRight.GetTransition(SmartPlug);
            smartPlugTransition.Completed += delegate { smartPlugTransition.Stop(); };
            smartPlugTransition.Begin();

            var chargeLimitTransition = slideLeft.GetTransition(ChargeLimit);
            chargeLimitTransition.Completed += delegate { chargeLimitTransition.Stop(); };
            chargeLimitTransition.Begin();

            var slideUp = new SlideTransition { Mode = SlideTransitionMode.SlideUpFadeIn };
            var chargeTransition = slideUp.GetTransition(Charge);
            chargeTransition.Completed += delegate { chargeTransition.Stop(); };
            chargeTransition.Begin();
        }

        private void ApplicationBarMenuItem_Click(object sender, System.EventArgs e)
        {
            NavigationService.Navigate(ViewNames.AboutView);
        }

        private void Edit(object sender, EventArgs e)
        {
            NavigationService.Navigate(ViewNames.EditSettingsView);
        }

        private async void Cancel(object sender, EventArgs e)
        {
            if(ViewModel.CanCancelCharge())
                await ViewModel.CancelCharge();
        }
    }
}